﻿using System;
using System.Collections.Generic;
using Omniscient.Foundation.Contrib.Data.MySql;
using Omniscient.Foundation.Data;
using MySql.Data.MySqlClient;
using Omniscient.Foundation.ApplicationModel;

namespace Omniscient.TimeTracker.Data.Adapters.MySql
{
    public class CategoryAdapter : IEntityAdapter<Category>
    {
        IConnectivity _connectivity;

        public CategoryAdapter() : this(ApplicationManager.Current.ServiceProvider.GetService<IConnectivity>()) { }

        public CategoryAdapter(IConnectivity connectivity)
        {
            if (connectivity == null) throw new ArgumentNullException("The adapter cannot work with no connectivity.  Null argument: connectivity.");
            _connectivity = connectivity;
        }

        #region IEntityAdapter<Category> Members

        public EntityList<Category> LoadAll()
        {
            MySqlConnection cn;
            MySqlCommand cmd;
            MySqlDataReader rd;
            EntityList<Category> categories;

            categories = new EntityList<Category>();
            cn = _connectivity.CreateConnection();

            string sql = @"SELECT CategoryGuid, Name, Description FROM category";

            try
            {
                cmd = cn.CreateCommand();
                cmd.CommandText = sql;
                cn.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    Category c;
                    Guid id;
                    id = new Guid(rd.GetString("CategoryGuid"));
                    c = new Category(id, true);
                    c.Name = rd.GetString("Name");
                    c.Description = rd.GetString("Description");
                    categories.Add(c);
                }
                rd.Close();
                return categories;
            }
            finally
            {
                cn.Dispose();
            }
        }

        public EntityList<Category> LoadByForeignKey(string propertyName, Guid id)
        {
            throw new NotImplementedException();
        }

        public Category LoadByKey(Guid id)
        {
            throw new NotImplementedException();
        }

        public EntityList<Category> LoadByObjectQuery(Omniscient.Foundation.Data.ObjectQuery.OQuery<Category> query)
        {
            throw new NotImplementedException();
        }

        public EntityList<Category> LoadByQuery(string queryName)
        {
            throw new NotImplementedException();
        }

        public EntityList<Category> LoadByValueProperty(string propertyName, object value)
        {
            throw new NotImplementedException();
        }

        public void Save(IEnumerable<Category> entities)
        {
            throw new NotImplementedException();
        }

        public void Save(Category entity)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
